#---
# Excerpted from "Rails for PHP Developers",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material, 
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose. 
# Visit http://www.pragmaticprogrammer.com/titles/ndphpr for more book information.
#---
class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :initialize_user

  # make these available as ActionView helper methods.
  helper_method :logged_in?

  protected

  # Check if the user is already logged in
  def logged_in?
    @current_user.is_a?(User)
  end

  # setup user info on each page
  def initialize_user
    @current_user = User.find_by_id(session[:user]) if session[:user]
  end

  
  def admin_required
    unless admin?
      redirect_to :controller => 'sessions', :action => 'new'
    end
  end
  
end
